package batch.common.mail;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailSender {
    public static boolean sendMail(String sServer, String sFrom, String sTo, String sSubject, String sBody, String sMimeType, String sSubjectEncoding) {
        Properties properties = new Properties();
        properties.put("mail.smtp.host", sServer);
        Session session = Session.getDefaultInstance(properties, null);
        session.setDebug(false);

        boolean bResult = true;
        try {
            MimeMessage mimemessage = new MimeMessage(session);
            mimemessage.setFrom(new InternetAddress(sFrom));
            mimemessage.setRecipients(javax.mail.Message.RecipientType.TO,
                    InternetAddress.parse(sTo));
            mimemessage.setSubject(sSubject, sSubjectEncoding);
            mimemessage.setSentDate(new Date());
            mimemessage.setContent(sBody, sMimeType);

            Transport.send(mimemessage);
        }
        catch (Exception e) {
            e.printStackTrace();
            bResult = false;
        }

        return bResult;
    }

    public static boolean sendMail(String sFrom, String sTo, String sSubject, String sBody, String sMimeType) {
        return sendMail("localhost", sFrom, sTo, sSubject, sBody, sMimeType, "ISO-8859-1");
    }

    public static boolean sendMail(String sFrom, String sTo, String sSubject, String sBody) {
        return sendMail("localhost", sFrom, sTo, sSubject, sBody, "text/plain", "ISO-8859-1");
    }
    
    public static boolean sendAttachMail(String sFrom, String sTo, String sSubject, String sBody, String sEncoding, String sFileName) {
        return sendAttachMail("localhost", sFrom, sTo, sSubject, sBody, sEncoding, sFileName);
    }    
    
    public static boolean sendAttachMail(String sServer, String sFrom, String sTo, String sSubject, String sBody, String sEncoding, String sFileName) {


    	
    	Properties properties = new Properties();
        properties.put("mail.smtp.host", sServer);
        Session session = Session.getDefaultInstance(properties, null);
        session.setDebug(false);
        
        boolean bResult = true;
        try {
        	
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(sFrom));
            msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(sTo));
            msg.setSubject(sSubject, sEncoding);
            
        	// create and fill the first message part
        	MimeBodyPart mbp1 = new MimeBodyPart();
        	mbp1.setText(sBody, sEncoding);
        	
        	// create the second message part
        	MimeBodyPart mbp2 = new MimeBodyPart();
        	
        	//mbp2.attachFile(sFileName);
        	
        	// attach the file to the message
        	FileDataSource fds = new FileDataSource(sFileName);
        	mbp2.setDataHandler(new DataHandler(fds));
        	mbp2.setFileName(fds.getName());
        	
        	// create the Multipart and its parts to it
        	Multipart mp = new MimeMultipart();
        	mp.addBodyPart(mbp1);
        	mp.addBodyPart(mbp2);
        	
        	// add the Multipart to the message
        	msg.setContent(mp);
            
        	msg.setSentDate(new Date());

            Transport.send(msg);
        }
        catch (Exception e) {
            e.printStackTrace();
            bResult = false;
        }

        return bResult;
    }    
}
